<?php

/**
 * Created by PhpStorm.
 * User: taizi
 * Date: 2017/4/20
 * Time: 11:29
 */
class ReserveApp extends BackendApp
{
    var $_reserve_mod;

    function __construct()
    {
        $this->ReserveApp();
    }

    function ReserveApp()
    {
        parent::BackendApp();
        $this->_reserve_mod =& m('reserve');

    }

    function index()
    {
        $db =& db();
        $conditions = $this->_get_query_conditions(array(

            array(
                'field' => 'th_time',
                'equal' => '>=',
                'name' => 'th_time_from',
                'handler' => 'gmstr2time',

            ),
            array(
                'field' => 'th_time',
                'equal' => '<=',
                'name' => 'th_time_to',
                'handler' => 'gmstr2time_end',

            ),
            array(
                'field' => 'add_time',
                'equal' => '>=',
                'name' => 'add_time_from',
                'handler' => 'gmstr2time',

            ),
            array(
                'field' => 'add_time',
                'equal' => '<=',
                'name' => 'add_time_to',
                'handler' => 'gmstr2time_end',
            )

        ));
        if ($_GET['member_value'] != '') {
            $member_value = $_GET['member_value'];
            $conditions .= " AND (member.user_name like '%" . $member_value . "%' 
            or reserve.vhccode like '%" . $member_value . "%'
            or reserve.tel like '%" . $member_value . "%'
            )";
            $this->assign('member_value', $member_value);
        }

        if ($_GET['box_value'] != '') {
            $box_value = $_GET['box_value'];
            $conditions .= " AND reserve.box like '%" . $box_value . "%'";
            $this->assign('box_value', $box_value);
        }
        if ($_GET['ship_value'] != '') {
            $ship_value = $_GET['ship_value'];
            $conditions .= " AND (reserve.company like '%" . $ship_value . "%'
            or reserve.station like '%" . $ship_value . "%'
            )";
            $this->assign('ship_value', $ship_value);
        }
        if ($_GET['ship1_value'] != '') {
            $ship1_value = $_GET['ship1_value'];
            $conditions .= " AND (reserve.ship_name like '%" . $ship1_value . "%'
            or reserve.ship_code like '%" . $ship1_value . "%'
            or reserve.voyage like '%" . $ship1_value . "%'
            or reserve.bill like '%" . $ship1_value . "%'
            )";
            $this->assign('ship1_value', $ship1_value);
        }

        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc'))) {
                $sort = 'add_time';
                $order = 'desc';
            }
        } else {
            $sort = 'add_time';
            $order = 'desc';
        }
        $page = $this->_get_page();
        $reserves = $this->_reserve_mod->find(array(
            'join' => 'belongs_to_member',
            'fields' => 'this.*,member.user_name',
            'conditions' => '1=1' . $conditions,
            'limit' => $page['limit'],
            'order' => "$sort $order",
            'count' => true,

        ));

        $page['item_count'] = $this->_reserve_mod->getCount();
        $this->_format_page($page);
        $sql = "select d.d_key,d.d_value from zys_dictionary as d WHERE d.d_code='boxtype'";
        $boxtypes = $db->getAll($sql);
        $box_values = array();
        foreach ($boxtypes as $key => $val) {
            $box_values[$val['d_key']] = $val['d_value'];
        }
        $this->import_resource(array(
            'script' => 'jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->assign('filtered', $conditions ? 1 : 0);
        $this->assign('page_info', $page);
        $this->assign('box_values', $box_values);
        $this->assign('reserves', $reserves);
        $this->display('reserve.index.html');

    }


}